package projetolp2.GraphicInterface;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import projetolp2.Sistem.Sistema;

public class Cadastrar extends MealPoints implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String nomePadrao = "Nome";
	private JPanel panelMenu = new JPanel(new BorderLayout());
	private JPanel panel1 = new JPanel(new GridLayout(0, 1));
	private JPanel panel2= new JPanel(new GridLayout(0, 1));
	private JPanel panel3= new JPanel(new GridLayout(0, 1));
	private JPanel panel4 = new JPanel(new GridLayout(0, 1));
	private JButton VOLTAR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getVoltar());
	private JButton PROSSEGUIR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getProsseguir());
	protected JTextField usuario = new JTextField(nomePadrao);
	final JLabel label = new JLabel("Digite o nome do usuario:");
	private JPanel panelFinal = new JPanel(new GridLayout(1,1));
	
	public Cadastrar(){
		
	}private void ajudaDeBotoes(){
		VOLTAR.setToolTipText("Voltar a janela anterior");
		PROSSEGUIR.setToolTipText("Continuar processo de cadastramento");
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == VOLTAR){
			removerAdicionar(cadastrar,menu );	
		}
		else if(e.getSource() == PROSSEGUIR){
			int contador = 0;
			for(int i = 1;i<usuario.getText().length()+1;i++){
				if(",.:;/?1234567890()*&%$#!@|[]{}-_<>".contains(usuario.getText().split("")[i])){
					contador = 10;
					if(contador > 1){
						JOptionPane.showMessageDialog(null,"Caracteres inválidos");
						break;
					}
				}
			}
			try {
				for(String i : SISTEMA.nomeDeUsuarios()){
					if(i.replace(" ", "").equals(usuario.getText().replace(" ", ""))){
						contador = 10;
						JOptionPane.showMessageDialog(null,"Usu�rio ja cadastrado");
						break;
					}
				}
			} catch (HeadlessException e1) {
				e1.printStackTrace();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			if(contador<=1){
				CadastrarPag2.mudaNomeUsuario(usuario.getText());
				removerAdicionar(cadastrar,cadastrarPag2 );	
			}
		}
	}
	private void createComponents() {
		ajudaDeBotoes();
		
		VOLTAR.setMnemonic(KeyEvent.VK_I);
        VOLTAR.addActionListener(this);
        label.setLabelFor(VOLTAR);
        
        PROSSEGUIR.setMnemonic(KeyEvent.VK_I);
        PROSSEGUIR.addActionListener(this);
        label.setLabelFor(PROSSEGUIR);

        panel1.add(label);
        panel2.add(usuario);
        panel3.add(PROSSEGUIR);
        panel4.add(VOLTAR);
        panel1.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                0) //right
                );
        panel2.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                0) //right
                );
        panel3.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                30, //left
                10, //bottom
                10) //right
                );
        panel4.setBorder(BorderFactory.createEmptyBorder(
        		10, //top
                10, //left
                10, //bottom
                30) //right
                );
        
        panelFinal.add(panel1);
        panelFinal.add(panel2);
        panelFinal.add(panel3);
        panelFinal.add(panel4);
        panelMenu.add(panelFinal, BorderLayout.NORTH);
    }
	public Component panel(){
		createComponents();
		panelMenu.setVisible(true);
		
		
		return panelMenu;
	}	
}
